package com.itheima.cotroller;

import com.itheima.common.entity.Result;
import com.itheima.common.utils.POIUtils;
import com.itheima.pojo.OrderSetting;
import com.itheima.sercive.OrderSettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.util.StreamUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/ordersetting")
public class OrderSettingController {

    @Autowired
    private OrderSettingService orderSettingService;

        @GetMapping("/download")
        public void downloadTemplate(HttpServletResponse response) throws Exception {
            ClassPathResource resource = new ClassPathResource("templates/ordersetting_template.xlsx");
            InputStream in = resource.getInputStream();

            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            response.setHeader("Content-Disposition", "attachment; filename=ordersetting_template.xlsx");

            StreamUtils.copy(in, response.getOutputStream());
            in.close();
        }

        @PostMapping("/upload")
        public Result upload(@RequestParam("excelFile") MultipartFile file) {
            try {
                // 使用POI工具类读取上传的Excel文件内容，返回每行数据组成的字符串数组列表
                List<String[]> rows = POIUtils.readExcel(file);
                // 创建一个用于存储预约设置对象的列表
                List<OrderSetting> list = new ArrayList<>();
                // 定义日期格式化工具，用于将字符串转换为Date类型
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

                // 遍历每一行数据
                for (String[] row : rows) {
                    // 为每一行创建一个OrderSetting对象
                    OrderSetting os = new OrderSetting();
                    // 将第一列的字符串解析为日期，并设置到OrderSetting对象中
                    os.setOrderDate(sdf.parse(row[0]));
                    // 将第二列的字符串转换为整数，作为可预约人数
                    os.setNumber(Integer.parseInt(row[1]));
                    // 设置初始已预约人数为0
                    os.setReservations(0);
                    // 将构造好的OrderSetting对象添加进列表
                    list.add(os);
                }

                // 调用服务层方法批量保存预约设置信息
                orderSettingService.saveBatch(list);
                // 如果成功执行，返回成功消息
                return Result.success("预约设置成功");
            } catch (Exception e) {
                // 捕获异常并返回错误消息，包含具体的错误原因
                return Result.error("上传失败: " + e.getMessage());
            }
        }

        @GetMapping("/getOrderSettingByMonth")
        public Result getOrderSettingByMonth(@RequestParam String month) {
            return orderSettingService.getOrderSettingByMonth(month);
        }

        @PutMapping("/editNumberByOrderDate")
        public Result editNumber(@RequestBody OrderSetting orderSetting) {
            return orderSettingService.editNumberByDate(orderSetting);
        }
}